package com.ccrfid.rmc6;

import com.ccrfid.rmc6.lbservice3.mq.MqMsgDev;
import com.ccrfid.rmc6.lbservice3.pojo.msg.MsgType;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class MockData {
    public static void main(String[] args) {
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("failover:(tcp://localhost:61616)");
        Connection connection = null;
        try {
            connection = connectionFactory.createConnection();
            connection.start();
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createTopic("mp_devices_real");
            MessageProducer messageProducer = session.createProducer(destination);
            messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

            ObjectMapper objectMapper = new ObjectMapper();

            List<MqMsgDev> msgList = createTestMsg();
            try {
                String msgContent = objectMapper.writeValueAsString(msgList);
                messageProducer.send(session.createTextMessage(msgContent));
                System.out.println("mp_devices_real:" + msgContent);
            } catch (JsonProcessingException ex) {
                ex.printStackTrace();
            }
            try {
                TimeUnit.MILLISECONDS.sleep(200);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }

            messageProducer.close();
            session.close();

        } catch (JMSException ex) {
            ex.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (JMSException ex) {
                    ex.printStackTrace();
                    System.out.println("Could not close an open connection...");
                }
            }
        }
    }

    public static List<MqMsgDev> createTestMsg() {
        List<MqMsgDev> list = new ArrayList<>();

        list.add(createLocateMqMsgDev(101, 1L));
        list.add(createLocateMqMsgDev(101, 2L));
        list.add(createLocateMqMsgDev(101, 3L));
//        list.add(createLocateMqMsgDev(102, 4));
//        list.add(createLocateMqMsgDev(102, 5));
//        list.add(createLocateMqMsgDev(102, 6));
//        list.add(createLocateMqMsgDev(102, 7));
//        list.add(createLocateMqMsgDev(103, 8));

        return list;
    }

    public static MqMsgDev createLocateMqMsgDev(Integer locatorNumber, Long cardNumber) {
        MqMsgDev mqMsg = new MqMsgDev();
        mqMsg.setLocatorNumber(locatorNumber);
        mqMsg.setCardNumber(cardNumber);
        mqMsg.setMsgType(MsgType.DEVICE_MESSAGE_TYPE_ENTERLOCATOR);
        mqMsg.setReaderNumber(0);
        mqMsg.setArg1(0);
        mqMsg.setArg2(0);
        mqMsg.setTime(new Date().getTime());
        return mqMsg;
    }
}
